/*
 * $Id: MyReloadableResourceBundleMessageSource.java 42 2013-04-17 15:33:59Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 42 $
 * $Date: 2013-04-17 15:33:59 +0000 (Wed, 17 Apr 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.service.support;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

/**
 * The class <code>MyReloadableResourceBundleMessageSource</code> represents the custom reloadable message source.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 42 $
 */
public class MyReloadableResourceBundleMessageSource extends ReloadableResourceBundleMessageSource {
    /**
     * The logger.
     */
    private Logger logger;

    /**
     * The edited messages.
     */
    private Map<Locale, Map<String, String>> editedMessages;

    /**
     * The default constructor.
     */
    public MyReloadableResourceBundleMessageSource() {
        logger = LoggerFactory.getLogger(getClass());
        editedMessages = new HashMap<Locale, Map<String, String>>();
    }

    /**
     * Returns the edited message.
     *
     * @param code The code.
     * @param locale The locale.
     *
     * @return The edited message.
     */
    public String getEditedMessage(String code, Locale locale) {
        Map<String, String> codeToMessageMap;
        if ((codeToMessageMap = getCodeToEditedMessageMap(locale)) != null) {
            return codeToMessageMap.get(code);
        }
        return null;
    }

    /**
     * Returns the edited messages.
     *
     * @return The edited messages.
     */
    public Map<Locale, Map<String, String>> getEditedMessages() {
        return editedMessages;
    }

    /**
     * Returns the edited messages for the given locale.
     *
     * @param locale The locale.
     *
     * @return The edited messages for the given locale.
     */
    public Map<String, String> getEditedMessages(Locale locale) {
        return getCodeToEditedMessageMap(locale);
    }

    /**
     * Initializes the edited messages.
     *
     * @param messages The messages.
     */
    public void setEditedMessages(Map<Locale, Map<String, String>> messages) {
        editedMessages = messages;
    }

    /**
     * Returns the original message.
     *
     * @param code The code.
     * @param locale The locale.
     *
     * @return The original message.
     */
    public String getOriginalMessage(String code, Locale locale) {
        return getMergedProperties(locale).getProperty(code);
    }

    public Map<String, String> getOriginalMessages(Locale locale) {
        Map<String, String> messages = new HashMap<String, String>();

        Properties properties = getMergedProperties(locale).getProperties();

        for (Object code : properties.keySet()) {
            messages.put((String) code, (String) properties.get(code));
        }

        return messages;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.springframework.context.support.ReloadableResourceBundleMessageSource#resolveCode(String, java.util.Locale)
     */
    @Override
    protected MessageFormat resolveCode(String code, Locale locale) {
        String message;
        if ((message = getEditedMessage(code, locale)) != null) {
            return createMessageFormat(message, locale);
        }
        return super.resolveCode(code, locale);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.springframework.context.support.ReloadableResourceBundleMessageSource#resolveCodeWithoutArguments(String, java.util.Locale)
     */
    @Override
    protected String resolveCodeWithoutArguments(String code, Locale locale) {
        String message;
        if ((message = getEditedMessage(code, locale)) != null) {
            return message;
        }
        return super.resolveCodeWithoutArguments(code, locale);
    }

    /**
     * Returns the code to edited messages map.
     *
     * @param locale The locale.
     *
     * @return The code to edited messages map.
     */
    private Map<String, String> getCodeToEditedMessageMap(Locale locale) {
        if (editedMessages.size() > 0) {
            //return editedMessages.get(locale.equals(Locale.getDefault()) ? null : locale);
            Map<String, String> codeToMessageMap;
            if (locale.equals(Locale.getDefault()) || (codeToMessageMap = editedMessages.get(locale)) == null) {
                codeToMessageMap = editedMessages.get(null);
            }
            return codeToMessageMap;
        }
        return null;
    }
}
